#define _CRT_SECURE_NO_WARNINGS 1


#include<algorithm>
#include<vector>
#include<string>
#include<iostream>

using namespace std;

int main()
{
    int n = 0;
    cin >> n;
    int Min = 999999;
    if (n < 2)
    {
        cout << -1 << endl;
        return 0;
    }
    string s1, s2;
    vector<string> v;
    cin >> s1 >> s2;
    if (s1 == "" || s2 == "")
    {
        cout << -1 << endl;
        return 0;
    }
    int a = -1, b = -1;
    for (int i = 0; i < n; i++)
    {
        string temp;
        cin >> temp;
        if (temp == s1)
        {
            a = i;
        }
        if (temp == s2)
        {
            b = i;
        }
        v.push_back(temp);
    }
    if (a == -1 || b == -1)
    {
        cout << -1 << endl;
    }
    cout << abs(b - a) << endl;
    /*if (a == 0 || b == 0)
    {
        cout << -1 << endl;
        return 0;
    }
    int begin = 0, end = 0;
    while (end < v.size() && begin < v.size())
    {
        while (v[begin] != s1 && begin <= a)
        {
            begin++;
        }
        while (v[end] != s2 && end <= b)
        {
            end++;
        }
        if (abs(begin - end) < Min && v[begin] == s1 && v[end] == s2)
        {
            Min = abs(begin - end);
        }
        begin++;
        end++;
    }
    cout << Min << endl;*/

    return 0;
}